<?php
/**
 * Ecart
 * 
 * This file is part of Ecart.
 * 
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @category    Ecart
 * @package     Ecart_Catalog
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 * 
 * @category    Ecart
 * @package     Ecart_Catalog
 * @subpackage  Model
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Catalog_Model_Product_Option_Value extends Ecart_Db_Table
{
    protected $_name = 'catalog_product_option_value';
    protected $_dependentTables = array('Ecart_Catalog_Model_Product_Option_Value_Text');
    protected $_referenceMap    = array(
        'Option' => array(
            'columns'           => 'option_id',
            'refTableClass'     => 'Ecart_Catalog_Model_Product_Option',
            'refColumns'        => 'id'
        )
    );
    
    /**
     * Add new or Update existing option value
     * 
     * @param array $data Array of values
     *  Example: array(
     *      valueId => array(
     *          id,
     *          valueset_id,
     *          sort_order,
     *          name_langId
     *      )
     *  )
     * @return bool
     */
    public function save(array $data)
    {
        $modelValueText = Ecart::single('catalog/product_option_value_text');
        $languageIds = array_keys(Ecart_Collect_Language::collect());
        
        foreach ($data as $values) {
            if (isset($values['id'])) {// update
                $this->update(
                    array('sort_order' => $values['sort_order']), 
                    'id = ' . intval($values['id'])
                );
                foreach ($languageIds as $langId) {
                    if (!$row = $modelValueText->find(
                            intval($values['id']),
                            intval($langId)
                        )->current()) {

                        $row = $modelValueText->createRow(array(
                            'option_value_id' => intval($values['id']),
                            'language_id' => intval($langId),
                            'name' => $values['name_' . $langId]
                        ));
                    } else {
                        $row->setFromArray(array(
                            'name' => $values['name_' . $langId]
                        ));
                    }
                    $row->save();
                }
            } else { // insert
                $values['id'] = $this->insert(array(
                    'valueset_id' => $values['valueset_id'],
                    'sort_order' => $values['sort_order'])
                );
                foreach ($languageIds as $langId) {
                    $modelValueText->insert(array(
                        'option_value_id' => $values['id'],
                        'language_id' => $langId,
                        'name'        => $values['name_' . $langId]
                    ));
                }
            }
        }
        Ecart::message()->addSuccess(
            Ecart::translate('core')->__(
                'Data was saved successfully'
            )
        );
        return true;
    }
    
    public function getByText($valueText, $valuesetId)
    {
        $select = $this->select()
            ->from(array('v' => $this->_prefix . 'catalog_product_option_value'), 'id')
            ->join(array('vt' => $this->_prefix . 'catalog_product_option_value_text'),
                'vt.option_value_id = v.id',
                array())
            ->where('v.valueset_id = ?', $valuesetId)
            ->where('vt.name = ?', $valueText)
            ->limit(1);
        $valueId = $this->getAdapter()->fetchOne($select);
        if ($valueId) {
            return $this->fetchRow(
                $this->select()->where('id = ?', $valueId)
            );
        }
        return $valueId;
    }
}